// <copyright file="BaseAutoCompleteHandler.cs" company="YUIAsp.Net">
// Copyright (c) 2009 All Right Reserved
// </copyright>
// <author>raul popescu</author>
// <email>dev@yuiasp.net</email>
// <date>2009-01-30</date>
// <summary>YUIAsp.Net File</summary>

using System.Web;

namespace YUIAspNet
{
    /// <summary>
    /// The base handler source for the AutoComplete.
    /// It can be used as a base class, which already exposes some functionality
    /// All you need to do in the implementing classes is to implement the GetResults method.
    /// </summary>
    public abstract class BaseAutoCompleteHandler : IHttpHandler
    {
        #region IHttpHandler Members

        /// <summary>
        /// Gets a value indicating whether another request can use the <see cref="T:System.Web.IHttpHandler"/> instance.
        /// </summary>
        /// <value></value>
        /// <returns>true if the <see cref="T:System.Web.IHttpHandler"/> instance is reusable; otherwise, false.</returns>
        public bool IsReusable
        {
            get { return false; } 
        }

        /// <summary>
        /// Enables processing of HTTP Web requests by a custom HttpHandler that implements the <see cref="T:System.Web.IHttpHandler"/> interface.
        /// </summary>
        /// <param name="context">An <see cref="T:System.Web.HttpContext"/> object that provides references to the intrinsic server objects (for example, Request, Response, Session, and Server) used to service HTTP requests.</param>
        public void ProcessRequest(HttpContext context)
        {
            string query = context.Request.QueryString["query"];

            int maxItems;
            if (!int.TryParse(context.Request.QueryString["query"], out maxItems))
                maxItems = 10;

            string code = context.Request.QueryString["code"];

            LocalDataSource src = GetResults(query, maxItems, code);

            context.Response.Write(src.Serialize());
        }

        /// <summary>
        /// Gets the results.
        /// </summary>
        /// <param name="query">The query.</param>
        /// <param name="maxItems">The max items.</param>
        /// <param name="autoCompleteCode">The auto complete code.</param>
        /// <returns></returns>
        public abstract LocalDataSource GetResults(string query, int maxItems, string autoCompleteCode);

        #endregion
    }
}
